import Vue from 'vue'
import Router from 'vue-router'


Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'login',
      component: () => import(/* webpackChunkName: "login" */ '@/components/login.vue')
    }, {
      path: '/home',
      name: 'home',
      component: () => import(/* webpackChunkName: "home" */ '@/components/home.vue'),
      redirect:"/users",
      children: [
        {
          path: '/users',
          name: 'users',
          component: () => import(/* webpackChunkName: "users" */ '@/components/users.vue'),
        }, {
          path: '/roles',
          name: 'roles',
          component: () => import(/* webpackChunkName: "roles" */ '@/components/roles.vue'),
        }, {
          path: '/rights',
          name: 'rights',
          component: () => import(/* webpackChunkName: "rights" */ '@/components/rights.vue'),
        }, {
          path: '/goods',
          name: 'goods',
          component: () => import(/* webpackChunkName: "goods" */ '@/components/goods.vue'),
        }, , {
          path: '/add',
          name: 'add',
          component: () => import(/* webpackChunkName: "add" */ '@/components/add.vue'),
        },{
          path: '/categories',
          name: 'categories',
          component: () => import(/* webpackChunkName: "categories" */ '@/components/categories.vue'),
        }, {
          path: '/params',
          name: 'params',
          component: () => import(/* webpackChunkName: "params" */ '@/components/params.vue'),
        }, {
          path: '/reports',
          name: 'reports',
          component: () => import(/* webpackChunkName: "reports" */ '@/components/reports.vue'),
        }, {
          path: '/orders',
          name: 'orders',
          component: () => import(/* webpackChunkName: "orders" */ '@/components/orders.vue'),
        }
      ]
    }
  ]
  
})
router.beforeEach((to, from, next) => {
  let token = sessionStorage.getItem("token")
  if (to.path == "/")return token?next("/home"):next();
  if(!token) next("/")
  next()
  
})
export default router
